//
//  MyPlayerViewController2.m
//  kankanVideo
//
//  Created by wangbo on 6/13/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MyPlayerViewController2.h"
#import <MediaPlayer/MediaPlayer.h>
#import "MyRequest.h"
#import "XMLParser.h"
#import "ASIHTTPRequest.h"

@implementation MyPlayerViewController2
@synthesize player;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void)dealloc{
    [player release];
    [super dealloc];
}

-(void)viewWillAppear:(BOOL)animated{
    NSLog(@"player_willappear and player_retaincount:%d",player.retainCount);
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(myMovieFinishedCallback:) name: MPMoviePlayerPlaybackDidFinishNotification object: nil];
}
-(void)playMovieURL:(NSString*)theURL{
    NSLog(@"playMovieURL_start:%@",theURL);
    [[MyRequest shareInstance]loaddataWithURL:theURL forObject:self];
    //MyRequest *request=[[MyRequest alloc]init];
    //[request loaddataWithURL:theURL forObject:self];
}
-(void)requestFinishedHandler:(ASIHTTPRequest*)request{
    NSMutableArray *arr=[XMLParser parseVideoDetail:request.responseData];
    //NSLog(@"movie_url:%@",[[arr objectAtIndex:0]objectAtIndex:0]);
    _videourl=(NSString*)[[arr objectAtIndex:0]objectAtIndex:0];
    [player playMovieAtURL:[NSURL URLWithString:_videourl]];
}
-(void)requestFailedHandler:(ASIHTTPRequest *)request{
    NSError *err=request.error;
    NSLog(@"%@",err);
    NSLog(@"failed");
}
-(void)viewWillDisappear:(BOOL)animated{
    [player release];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    [super viewWillDisappear:animated];
    NSLog(@"player_willdisappear");
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    //CGRect viewInsetRect = CGRectInset ([self.view bounds],0,0);
    NSLog(@"didRotate1");
    CGRect viewInsetRect=CGRectInset([self.view bounds], 0, 0);
	[player setFrame:viewInsetRect];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

#pragma mark handle MPMoviePlayerController notification callback
// When the movie is done, release the controller.
- (void) myMovieFinishedCallback: (NSNotification*)notification
{
    NSLog(@"notification====%@",notification);
    //[[MPMoviePlayerView shareMoviePlayer] removeFromSuperview];
    
    NSNumber *reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]; 
	switch ([reason integerValue]) 
	{
		case MPMovieFinishReasonPlaybackEnded:
            NSLog(@"MPMovieFinishReasonPlaybackEnded");
            break;
            
          
		case MPMovieFinishReasonPlaybackError:
            NSLog(@"An error was encountered during playback");
            //            [self performSelectorOnMainThread:@selector(displayError:) withObject:[[notification userInfo] objectForKey:@"error"] 
            //                                waitUntilDone:NO];
            //[[MPMoviePlayerView shareMoviePlayer] stop];
            NSLog(@"sharemovie_count:%d",[[MPMoviePlayerView shareMoviePlayer] retainCount]);
            [self dismissModalViewControllerAnimated:YES];
			break;
            
            
		case MPMovieFinishReasonUserExited:
            NSLog(@"Users ended the playback");
            //[[MPMoviePlayerView shareMoviePlayer] stop];
            NSLog(@"sharemovie_count:%d",[[MPMoviePlayerView shareMoviePlayer] retainCount]);
            [self dismissModalViewControllerAnimated:YES];
			break;
            
		default:
            NSLog(@"unknown error happened!");
            //[[MPMoviePlayerView shareMoviePlayer] stop];
            [self dismissModalViewControllerAnimated:YES];
			break;
	} // switch
    
}

@end
